package com.tangkeyon.common;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * @Author: tangkeyon@gmail.com
 * @Date: 2019/5/24 19:03
 * @Version 1.0
 */
public class TokenCache {
    public static final String TOKEN_PRIFIX="token_";
    public static LoadingCache<String,String> loadingCache= CacheBuilder.newBuilder().initialCapacity(10000).maximumSize(10000).expireAfterAccess(12, TimeUnit.DAYS)
            .build(new CacheLoader<String, String>() {
//当在缓存中getKey若没有对应的值就调用这个方法返回所需的值
                @Override
                public String load(String s) throws Exception {
                    return "null";
                }
            });

    public static void setKey(String key,String value){
        loadingCache.put(key,value);
    }

    public static  String getKey(String key){
        String value=null;
        try {
            value= loadingCache.get(key);
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

        return value;
    }



}
